package com.cn.transfer.jobhandler;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.cn.transfer.common.constant.KeyConstant;
import com.cn.transfer.common.http.ApiClient;
import com.cn.transfer.common.utils.ConnmeUtils;
import com.cn.transfer.common.vo.QiweiJian;
import com.cn.transfer.common.vo.Task;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.Header;
import org.apache.http.message.BasicHeader;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: weidian
 * @date: 2024/4/28 11:31
 * @Description: 简道云待办工作台显示快速修改
 */
@Slf4j
@Component
public class BacklogMianXxlJob {    //(废弃)
    @XxlJob("backlogMian")
    public void backlog(){
        List<QiweiJian> qiweiJians=new ArrayList<QiweiJian>();
        Header[] headers = ConnmeUtils.createHeaders();
        Map<String, Object> parameterCx = new HashMap<>();
        parameterCx.put("app_id", "62f07fc990967100072e5d81");
        parameterCx.put("entry_id", "6617b070bb5bc3dd0ba66196");
        boolean fale=true;
        String zh= "";
        do {
            if (!zh.equals("")){
                parameterCx.put("data_id", zh);
            }
            parameterCx.put("limit", 100);
            String jsonSale = JSONUtil.toJsonStr(parameterCx);
            JSONObject jdySale = ApiClient.sendPostRequest(KeyConstant.Jian_URL + "/v5/app/entry/data/list", headers, jsonSale);
            String data = jdySale.getStr("data");
            JSONArray objects = JSONUtil.parseArray(data);
            List<QiweiJian> qiweiJianList = JSONUtil.toList(objects, QiweiJian.class);
            if (qiweiJianList.size()==100){
                zh=qiweiJianList.get(qiweiJianList.size()-1).get_id();
            }else {
                fale=false;
            }
            qiweiJians.addAll(qiweiJianList);
        }while (fale);
        for (QiweiJian qiu  : qiweiJians) {

            Map<String, Object> parameter = new HashMap<>();
            parameter.put("username", qiu.getPost_addr());
            boolean more = true;
            int total = 0;
            while (more){
                parameter.put("skip", total);
                String jsonSale = JSONUtil.toJsonStr(parameter);
                JSONObject entries = ApiClient.sendPostRequest(KeyConstant.Jian_URL + "/v5/workflow/task/list", headers, jsonSale);
                String data = entries.getStr("tasks");
                more = Boolean.parseBoolean(entries.getStr("has_more"));
                JSONArray objects = JSONUtil.parseArray(data);
                List<Task> tasks = JSONUtil.toList(objects, Task.class);
                total+=tasks.size();
            }
            if (total!=0) {
                String gettoken = ConnmeUtils.getJandaoyyunToken();
                Header[] originalHeaders = new Header[1];
                originalHeaders[0] = new BasicHeader("Content-Type", "application/json;charset=utf-8");

                Map<String, Object> parameterWoker = new HashMap<>();
                parameterWoker.put("userid", qiu.getWechat_id());
                parameterWoker.put("type", "keydata");
                parameterWoker.put("agentid", "1000117");
                Map<String, Object> keydata = new HashMap<>();
                Map<String, Object> items = new HashMap<>();
                items.put("key","待审批");
                items.put("data",total);
                items.put("jump_url","https://wxwork.jiandaoyun.com/dashboard#/flow/internal/todo");
                keydata.put("items",items);
                parameterWoker.put("keydata", keydata);
                String jsonSaleWoker = JSONUtil.toJsonStr(parameterWoker);
                JSONObject entries = ApiClient.sendPostRequest("https://qyapi.weixin.qq.com/cgi-bin/agent/set_workbench_data?access_token=" + gettoken, originalHeaders, jsonSaleWoker);
                log.info("工作台写入+++++"+entries);
            }
        }



    }
}
